package com.jxsd.edu.cn.tools;

import java.util.ArrayList;
import java.util.List;


/**
 * 分页请求器
 * pageSize       页面大小
 * currentPage    当前页号
 * t              查询的条件
 * */
public class PageBean<T>
{
   private   int   pageSize=6;
   private   int   currentPage=1;
   
   public  PageBean(){}
   
 public  PageBean(int  pageSize)
 {
	 this.pageSize=pageSize;
 }
   
public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
}
public int getPageSize() {
	return pageSize;
}
public void setCurrentPage(int currentPage) {
	this.currentPage = currentPage;
}
public int getCurrentPage() {
	return currentPage;
}

//获取一个Page对象,并用PageBean对象初值化Page对象
public  Page<T>  getPage(String sql,Object[]  params)
{
   Page<T>  page=new Page<T>();
   page.setRowCount(sql,params);
   page.setPageSize(pageSize);
   page.setCurrent_page(currentPage);
   List<T>  list=new ArrayList<T>();
   page.setList(list);
  
   return  page;
}

//获取组装的分页查询sql语句
public  String  getPageSQl(String sql)
{
	StringBuilder  sq=new  StringBuilder(sql);
	//定位当前页在结果集中的起始位置
	int begin=0;
	if(getCurrentPage()>1)
	{
		begin=(getCurrentPage()-1)*getPageSize();
	}
	
	 int length=getPageSize();
	 //改装成分页查询语句
	 sq.append(" limit ");
	 sq.append(begin);
	 sq.append(" , ");
	 sq.append(length);
	 sql=sq.toString();
	 
	 System.out.println("Page  SQL:"+sql);
	 
    return  sql;
}
}
